package com.zhangyuji.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangyuji.dto.DishDto;
import com.zhangyuji.entity.Dish;
import com.zhangyuji.entity.DishFlavor;
import com.zhangyuji.mapper.DishMapper;
import com.zhangyuji.service.DishFlavorService;
import com.zhangyuji.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Service
@Transactional
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;
    @Override
    public boolean saveDishAndFlavor(DishDto dishDto) {
        this.save(dishDto);

        Long id = dishDto.getId();
        System.out.println(id);

        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(id);
        }
        System.out.println(flavors);
        return dishFlavorService.saveBatch(flavors);


    }

    @Override
    public DishDto queryDishAndFlavor(Long id) {
        Dish dish = this.getById(id);
        System.out.println(dish);
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,id);
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
        System.out.println(flavors);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
       /* //id
        dishDto.setId(dish.getId());
        //菜品名称
        dishDto.setName(dish.getName());
        //菜品分类id
        dishDto.setCategoryName(dish.getName());
        dishDto.setCategoryId(dish.getCategoryId());
        dishDto.setId(dish.getId());
        //菜品价格
        dishDto.setPrice(dish.getPrice());
        //商品码
        dishDto.setCode(dish.getCode());
        //图片
        dishDto.setImage(dish.getImage());
        //描述信息
        dishDto.setDescription(dish.getDescription());
        //0 停售 1 起售
        dishDto.setStatus(dish.getStatus());
        //顺序
        dishDto.setSort(dish.getSort());
        dishDto.setCreateTime(dish.getCreateTime());
        dishDto.setUpdateTime(dish.getUpdateTime());
        dishDto.setCreateUser(dish.getCreateUser());
        dishDto.setUpdateUser(dish.getUpdateUser());

        //是否删除
        dishDto.setIsDeleted(dish.getIsDeleted());*/

        dishDto.setFlavors(flavors);
        System.out.println(dishDto);
        return dishDto;
    }

    @Override
    public boolean updateDishAndFlavor(DishDto dishDto) {
        Long id = dishDto.getId();
        boolean flag = this.updateById(dishDto);
        if (flag){
            //先根据dish_id删除口味
            LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(DishFlavor::getDishId,id);
            boolean remove = dishFlavorService.remove(queryWrapper);
            if (!remove){
                return false;
            }
            //在添加口味
            List<DishFlavor> flavors = dishDto.getFlavors();
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(id);
            }
            boolean b = dishFlavorService.saveBatch(flavors);
            if (b){
                return true;
            }
            return false;
        }
        return false;
    }
}
